package com.lgmshare.k3.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.lgmshare.k3.R;
import com.lgmshare.component.app.LaraDialog;
import com.lgmshare.component.widget.wheelview.OnWheelChangedListener;
import com.lgmshare.component.widget.wheelview.WheelVerticalView;
import com.lgmshare.component.widget.wheelview.adapter.WheelListAdapter;
import com.lgmshare.component.widget.wheelview.adapter.WheelTextAdapter;

import java.util.List;

/**
 * 彷IOS单项选择控件
 * Created by lim.
 * Datetime: 2015/6/30.
 * Email: lgmshare@mgail.com
 */
public class WheelDialog extends LaraDialog {

    private static final int WHEEL_VISIBLE_ITEMS = 5;

    private TextView dialog_tv_title;
    private Button dialog_btn_cancel;
    private Button dialog_btn_ensure;
    private WheelVerticalView dialog_wheelview;

    private CharSequence mTitle;
    private View.OnClickListener mEnsureClickListener;
    private View.OnClickListener mCancelClickListener;

    private int mWheelSheetIndex = 0;
    private boolean mWheelCyclic = false;
    private WheelTextAdapter mWheelTextAdapter;
    private OnWheelChangedListener mWheelChangedListener;

    private List<String> mDataList;

    public WheelDialog(Context context) {
        super(context, R.style.CommonBottomSlideDialog);
    }

    public WheelDialog(Context context, int selecteIndex) {
        super(context, R.style.CommonBottomSlideDialog);
        mWheelSheetIndex = selecteIndex;
    }

    public WheelDialog(Context context, int selecteIndex, boolean cyclic) {
        super(context, R.style.CommonBottomSlideDialog);
        mWheelSheetIndex = selecteIndex;
        mWheelCyclic = cyclic;
    }

    @Override
    protected int onBindLayoutResId() {
        return R.layout.dialog_wheel;
    }

    @Override
    protected void initView() {
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.dialog_wheel);

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = LayoutParams.MATCH_PARENT;
        layoutParams.height = LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.BOTTOM;

        dialog_tv_title = findViewById(R.id.dialog_tv_title);

        dialog_btn_ensure = findViewById(R.id.dialog_btn_ensure);
        dialog_btn_ensure.setVisibility(View.VISIBLE);
        dialog_btn_ensure.setOnClickListener(mEnsureClickListener);

        dialog_btn_cancel = findViewById(R.id.dialog_btn_cancel);
        dialog_btn_cancel.setVisibility(View.VISIBLE);
        dialog_btn_cancel.setOnClickListener(mCancelClickListener);

        dialog_wheelview = findViewById(R.id.dialog_wheelview);
        dialog_wheelview.setViewAdapter(mWheelTextAdapter);// 设置显示数据
        dialog_wheelview.setCyclic(mWheelCyclic);// 可循环滚动
        dialog_wheelview.setVisibleItems(WHEEL_VISIBLE_ITEMS);
        dialog_wheelview.setCurrentItem(mWheelSheetIndex);// 初始化时显示的数据

        if (!TextUtils.isEmpty(mTitle)) {
            dialog_tv_title.setVisibility(View.VISIBLE);
            dialog_tv_title.setText(mTitle);
        }

        if (mEnsureClickListener == null) {
            dialog_btn_ensure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
        if (mCancelClickListener == null) {
            dialog_btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }

    @Override
    public void setTitle(int resId) {
        mTitle = getContext().getResources().getString(resId);
        if (dialog_tv_title != null) {
            dialog_tv_title.setText(resId);
            dialog_tv_title.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 添加标题
     *
     * @param title 标题
     */
    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        if (dialog_tv_title != null) {
            dialog_tv_title.setVisibility(View.VISIBLE);
            dialog_tv_title.setText(title);
        }
    }

    public void setOnCancelClickListener(View.OnClickListener listener) {
        mCancelClickListener = listener;
        if (dialog_btn_ensure != null) {
            dialog_btn_cancel.setVisibility(View.VISIBLE);
            dialog_btn_cancel.setOnClickListener(listener);
        }
    }

    public void setOnEnsureClickListener(View.OnClickListener listener) {
        mEnsureClickListener = listener;
        if (dialog_btn_ensure != null) {
            dialog_btn_ensure.setVisibility(View.VISIBLE);
            dialog_btn_ensure.setOnClickListener(listener);
        }
    }

    /**
     * 滚动选择项改变监听事件
     *
     * @param listener
     */
    public void setOnWheelChangedListener(OnWheelChangedListener listener) {
        mWheelChangedListener = listener;
        if (dialog_wheelview != null) {
            dialog_wheelview.addChangingListener(listener);
        }
    }

    public void setWheelSelecteIndex(int selecteIndex) {
        this.mWheelSheetIndex = selecteIndex;
        if (dialog_wheelview != null) {
            dialog_wheelview.setCurrentItem(selecteIndex);
        }
    }

    /**
     * 滚动是否循环
     *
     * @param cyclic
     */
    public void setWheelCyclic(boolean cyclic) {
        this.mWheelCyclic = cyclic;
        if (dialog_wheelview != null) {
            dialog_wheelview.setCyclic(cyclic);// 可循环滚动
        }
    }

    private void setWheelTextAdapter(WheelTextAdapter adapter) {
        this.mWheelTextAdapter = adapter;
        if (dialog_wheelview != null) {
            dialog_wheelview.setViewAdapter(adapter);
        }
    }

    public void setStringArrayAdapter(List<String> list, int resLayout, int resId) {
        mDataList = list;
        WheelListAdapter adapter = new MyAdapter(getContext(), list);
        adapter.setTextColor(getContext().getResources().getColor(R.color.gray));
        adapter.setTextSize(18);
        adapter.setItemResource(resLayout);
        adapter.setItemTextResource(resId);
        setWheelTextAdapter(adapter);
    }

    public int getWheelSheetIndex() {
        return dialog_wheelview.getCurrentItem();
    }

    public String getWheelSheetValue() {
        return mDataList.get(dialog_wheelview.getCurrentItem());
    }

    class MyAdapter extends WheelListAdapter<String> {

        /**
         * Constructor
         *
         * @param list the items
         */
        public MyAdapter(Context context, List<String> list) {
            super(context, list);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            TextView viewById = view.findViewById(itemTextResourceId);
            if (getItemText(index).length() >= 6) {
                viewById.setTextSize(14);
            } else if (getItemText(index).length() >= 8) {
                viewById.setTextSize(12);
            } else {
                viewById.setTextSize(getTextSize());
            }
            return view;
        }

        @Override
        protected CharSequence getItemText(List<String> list, int index) {
            if (index >= 0 && index < list.size()) {
                return list.get(index);
            }
            return null;
        }
    }
}
